﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace JqueryNet.FormControl.FormWebControl
{
    public class JListBox : JWebControl
    {
        ListBox listbox;
        public JListBox(ListBox listbox) : base(listbox) { this.listbox = listbox; }

        public override void Attr(string key)
        {
            switch (key.ToLower())
            {
                case "css":
                case "class":
                case "cssclass":
                    Value = listbox.CssClass;
                    return;
                case "height":
                    Value = listbox.Height;
                    return;
                case "width":
                    Value = listbox.Width;
                    return;
                case "text": 
                    Value = listbox.Text;
                    return;
                case "tooltip":
                case "title":
                    Value = listbox.ToolTip;
                    return;
                case "index":
                case "selectedindex":
                    Value = listbox.SelectedIndex;
                    return;
                case "value":
                    Value = listbox.SelectedValue;
                    return;
                default:
                    base.Attr(key); return;
            }
        }

        public override void Attr(string key, string value)
        {
            int b;
            int.TryParse(value, out b);
            switch (key.ToLower())
            {
                case "css":
                case "class":
                case "cssclass":
                    Value = listbox.CssClass;
                    return;
                case "height":
                    listbox.Height = b;
                    return;
                case "width":
                    listbox.Width = b;
                    return;
                case "text": 
                    listbox.Text = value;
                    return;
                case "tooltip":
                case "title":
                    listbox.ToolTip = value;
                    return;
                case "index":
                case "selectedindex":
                    listbox.SelectedIndex = b;
                    return;
                case "value":
                    listbox.SelectedValue = value;
                    return;
                default:
                    base.Attr(key, value); return;
            }

        }

        public override void Val()
        {
            List<ListItem> list = new List<ListItem>();
            foreach (ListItem item in listbox.Items)
            {
                if (item.Selected)
                {
                    list.Add(item);
                }
            }
            Value = list;
        }

        public override void Val(string value)
        {
            foreach (ListItem item in listbox.Items)
            {
                if (item.Value == value)
                {
                    item.Selected = true;
                }
            }
        }

        public override void Val(string[] values)
        {
            foreach (ListItem item in listbox.Items)
            {
                if (values.Contains(item.Value))
                {
                    item.Selected = true;
                }
            }
        }
    }
}
